<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="addItem" type="primary" icon="plus">新增</a-button>
      <a-button @click="copyItem" style="background-color: rgb(233, 175, 0);border: none;"
                type="primary" icon="plus">
        复制段位[增加]
      </a-button>
      <!-- 高级查询区域 -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="handleBatchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <template>
        <span style="float:right;">
          <a @click="searchQuery()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <a-button v-print="'#datatable'" ghost type="primary">打印</a-button>
        </span>
      </template>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="tableData"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <div>
      <a-modal :visible="copyItemIsVisible" @cancel="copyItemIsVisible = false" @ok="submitCopyItem" width="40%">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标准分项编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemCode">
              <a-input v-model="model.standItemCode" placeholder="请输入标准分项编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标准分项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItemName">
              <a-input v-model="model.standItemName" placeholder="请输入标准分项目名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标准分项目别名简称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItemAliasname">
              <a-input v-model="model.standItemAliasname" placeholder="请输入标准分项目别名简称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分数最低分(含)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItemLowestScore">
              <a-input-number v-model="model.standItemLowestScore" placeholder="请输入分数最低分(含)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分数最高分(不含)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItemHighestScore">
              <a-input-number v-model="model.standItemHighestScore" placeholder="请输入分数最高分(不含)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <!--            特殊限制-->
            <a-form-model-item label="特殊限制" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItSpecialRestrictions">
              <a-input-number v-model="model.standItSpecialRestrictions" placeholder="请输入特殊限制"
                              style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemOrder">
              <a-input-number v-model="model.standItemOrder" placeholder="请输入项目序号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-modal>
    </div>
    <!--新增-->
    <div>
      <a-modal :visible="insertItemVisible" @cancel="addCancel" @ok="submitAdd" width="40%">
        <h2 style="text-align: center">添加评分标准</h2>
        <!--            此处新增标准-->
        <div style="border:1px solid #000">
          <a-form-model-item label="标准名称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                             prop="achievementStandardName">
            <a-input v-model="distribution.achievementStandardName" placeholder="请输入标准名称"></a-input>
          </a-form-model-item>
          <a-form-model-item label="标准级别" :labelCol="labelCol" :wrapperCol="wrapperCol"
                             prop="achievementStandardTtype">
            <j-dict-select-tag type="list" v-model="distribution.achievementStandardTtype"
                               dictCode="achievement_standard_ttype" placeholder="请选择评分标准类型"
                                style='width: 50%'
            />
          </a-form-model-item>
          <a-form-model-item label="标准类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                             prop="achievementStandardPurposeType">
            <j-dict-select-tag type="list" v-model="distribution.achiStandPurposeType"
                               dictCode="achi_stand_purpose_type" placeholder="请选择标准用途类型" style='width: 50%'/>
          </a-form-model-item>

<!--            <a-col :span="12">-->
<!--              <a-form-model-item label="标准用途" :labelCol="labelCol" :wrapperCol="wrapperCol"-->
<!--                                 prop="achievementStandardPurpose">-->
<!--                <j-dict-select-tag disabled type="list" v-model="distribution.achievementStandardPurpose"-->
<!--                                   dictCode="achievement_standard_purpose" placeholder="请选择标准用途类型" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->

              <a-form-model-item label="显示序号" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                 prop="achievementStandardOrder">
                <a-input-number v-model="distribution.achievementStandardOrder" placeholder="请输入标准序号"
                                style="width: 50%" />
              </a-form-model-item>

<!--              <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">-->
<!--                <j-dict-select-tag type="list" v-model="distribution.izOpen"-->
<!--                                   dictCode="iz_open" placeholder="请选择是否启用" style="width: 50%"/>-->
<!--              </a-form-model-item>-->

<!--              <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">-->
<!--                <j-dict-select-tag v-model="distribution.sysOrgCode" placeholder="请选择所属部门"-->
<!--                                   dictCode="sys_depart,depart_name,org_code" style="width: 50%"/>-->
<!--              </a-form-model-item>-->

        </div>
        <!--          创建子项标准-->
        <h2 style="text-align: center">添加子项</h2>

        <a-form-item v-for="(domain , index ) in domains" :key="domain.key">
          <div style="border:1px solid #000">
            <a-form-model-item label="标准分项编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemCode">
              <a-input v-model="domain.standItemCode" placeholder="请输入标准分项编码"></a-input>
            </a-form-model-item>

            <a-form-model-item label="标准分项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItemName">
              <a-input v-model="domain.standItemName" placeholder="请输入标准分项目名称"></a-input>
            </a-form-model-item>
            <a-form-model-item label="标准分项目别名简称" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="standItemAliasname">
              <a-input v-model="domain.standItemAliasname" placeholder="请输入标准分项目别名简称"></a-input>
            </a-form-model-item>
            <a-row>
              <a-col :span="12">
                <a-form-model-item label="分数最低分(含)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                   prop="standItemLowestScore">
                  <a-input-number v-model="domain.standItemLowestScore" placeholder="请输入分数最低分(含)"
                                  style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="分数最高分(不含)" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                   prop="standItemHighestScore">
                  <a-input-number v-model="domain.standItemHighestScore" placeholder="请输入分数最高分(不含)"
                                  style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :span="8">
                <a-form-model-item label="特殊限制" :labelCol="labelCol" :wrapperCol="wrapperCol"
                                   prop="standItSpecialRestrictions">
                  <a-input-number v-model="domain.standItSpecialRestrictions" placeholder="请输入特殊限制"
                                  style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
                  <j-dict-select-tag type="list" v-model="domain.izOpen"
                                     dictCode="iz_open" placeholder="请选择是否启用"  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="项目序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standItemOrder">
                  <a-input-number v-model="domain.standItemOrder" placeholder="请输入项目序号" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-form-model-item style="text-align: center">
              <a-button @click="removeDomainsItem(domain)" type="danger">删除</a-button>
            </a-form-model-item>
          </div>
        </a-form-item>
        <a-form-model style="text-align: center">
          <a-button icon="plus" type="primary" @click="addDomainsItem">增加子项</a-button>
        </a-form-model>
      </a-modal>
    </div>
    <standard-item-for-grading-modal ref="modalForm" @ok="diyFlashTable"></standard-item-for-grading-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { mixinDevice } from '@/utils/mixin'
import { deleteAction, getAction, postAction } from '@api/manage'
import StandardItemForGradingModal from './modules/StandardItemForGradingModal'

export default {
  name: 'StandardItemForGrading',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    StandardItemForGradingModal
  },
  data() {
    return {
      model: {},
      domains: [],
      distribution: {
        achievementStandardPurpose: '93',
        izOpen: 1
      },
      examId: '',
      insertItemVisible: false,
      copyItemIsVisible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '标准名称',
          align: 'center',
          dataIndex: 'achievementStandardName'
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'achiStandPurposeType_dictText'
        },
        {
          title: '段位名称',
          align: 'center',
          dataIndex: 'standItemName'
        },
        {
          title: '简称',
          align: 'center',
          dataIndex: 'standItemAliasname'
        },
        {
          title: '分数最低分[>=]',
          align: 'center',
          dataIndex: 'standItemLowestScore'
        },
        {
          title: '分数最高分(<)',
          align: 'center',
          dataIndex: 'standItemHighestScore'
        },
        {
          title: '顺序',
          align: 'center',
          sorter: true,
          dataIndex: 'standItemOrder'
        },

        {
          title: '特殊限制',
          align: 'center',
          dataIndex: 'standItSpecialRestrictions_dictText'
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'izOpen_dictText'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/achachievestandarditem/achAchieveStandardItem/list',
        delete: '/achachievestandarditem/achAchieveStandardItem/delete',
        deleteBatch: '/achachievestandarditem/achAchieveStandardItem/deleteBatch',
        exportXlsUrl: '/achachievestandarditem/achAchieveStandardItem/exportXls',
        importExcelUrl: 'achachievestandarditem/achAchieveStandardItem/importExcel'
      },
      tableData: []
    }
  },
  methods: {
    loadData(arg, examId) {
      if (arg === 1) {
        this.ipagination.current = 1
      }
      let searchUrl = '/achachievestandarditem/achAchieveStandardItem/queryAchievementsItemByRelObjIdType'
      let params = {
        relObjId: examId,
        achievementStandardPurpose: '93',
        pageNo: this.ipagination.current,
        pageSize: this.ipagination.pageSize
      }
      console.log('执行刷新' + examId)
      if (examId != null) {
        getAction(searchUrl, params).then(res => {
          if (res.success) {
            this.tableData = []
            this.tableData = res.result.records
            this.ipagination.total = res.result.total
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    getOrderMain(examId) {
      this.examId = examId
      this.loadData(1, examId)
    },
    copyItem() {
      let size = this.selectionRows.length
      if (size == 1) {
        this.copyItemIsVisible = true
        this.model = this.selectionRows[0]
      } else if (size > 1) {
        this.$message.error('每次仅允许复制一条数据')
      } else {
        this.$message.error('请选择一项后进行复制')
      }
    },
    submitCopyItem() {
      let params = this.model
      postAction('/achachievestandarditem/achAchieveStandardItem/copyItem', params).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.copyItemIsVisible = false
          this.loadData(1, this.examId)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    addItem() {
      if (this.examId != null && this.examId != '') {
        this.insertItemVisible = true
        this.distribution = {
          izOpen: 1,
          achievementStandardPurpose: '93'
        }
      } else {
        this.$message.error('请选择一次考试')
      }
    },
    submitAdd() {
      if (this.domains.length > 0) {
        this.distribution.delFlag = 0
        let arr = {
          standard: this.distribution,
          item: this.domains,
          examId: this.examId
        }
        let prams = {
          json: JSON.stringify(arr)
        }
        postAction('/achachievestandarditem/achAchieveStandardItem/insetNewItem', prams).then(res => {
          if (res.success) {
            this.addCancel()
            this.$message.success(res.message)
            this.loadData(1, this.examId)
            this.resetParams()
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    resetParams() {
      this.distribution = ''
      this.domains = ''
    },
    addCancel() {
      this.insertItemVisible = false
      this.model = {}
      this.distribution = {}
    },
    addDomainsItem() {
      this.domains.push({
        key: Date.now(),
        izOpen: 1,
        delFlag: 0
      })
    },
    removeDomainsItem(item) {
      let index = this.domains.indexOf(item)
      if (index != -1) {
        this.domains.splice(index, 1)
      }
    },
    diyFlashTable() {
      this.loadData(1, this.examId)
      this.onClearSelected()
    },
    handleBatchDel() {
      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function() {
            that.loading = true
            deleteAction(that.url.deleteBatch, { ids: ids }).then((res) => {
              if (res.success) {
                //重新计算分页问题
                // that.reCalculatePage(that.selectedRowKeys.length)
                this.$message.success(res.message)
                this.loadData(1, this.examId)
                this.onClearSelected()
              } else {
                this.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>